export default {
	methods: {
		qnUploadFile({ files, formData, url = 'https://up.qiniup.com', prefixUrl, name = 'file' }) {
			this.$uni.showLoading({ title: 'uploading' })
			let upFiles = [].concat(files)
			let uploads = []
			return new Promise((resolve, reject) => {
				const _uploadFile = async () => {
					const filePath = upFiles.shift()
					if (!filePath) {
						this.$uni.hideLoading()
						resolve(uploads)
						return
					}
					try {
						let { data:{src} } = await this.$http.upload(url, {
							filePath,
							name,
							formData,
							custom: { isAuth: false, resValidateCode: false, tipConfig: { storeLoading: true, isLoading: false, isErrorTip: false } }
						})
						// let fileUrl = prefixUrl + '/' + key
						let fileUrl = src
						uploads = [...uploads, fileUrl]
						_uploadFile()
					} catch (error) {
						this.$uni.showToast({ title: 'uploading error!' })
						reject(error)
					}
				}
				_uploadFile()
			})
		},
		uploadFile({ files, name = 'file', formData, url = 'user.upload' }) {
			this.$uni.showLoading({ title: 'uploading' })
			let upFiles = [].concat(files)
			let uploads = []
			return new Promise((resolve, reject) => {
				const _uploadFile = async () => {
					const filePath = upFiles.shift()
					if (!filePath) {
						this.$uni.hideLoading()
						resolve(uploads)
						return
					}
					try {
						let { data } = await this.$http.upload(url, {
							filePath,
							name,
							formData,
							custom: { tipConfig: { storeLoading: true, isLoading: false, isErrorTip: false } }
						})
						uploads = [...uploads, data]
						_uploadFile()
					} catch (error) {
						this.$uni.showToast({ title: 'uploading error!' })
						reject(error)
					}
				}
				_uploadFile()
			})
		}
	}
}
